package com.jz.beijianbao.subclass.controller;


import com.jz.beijianbao.subclass.bean.StatusVo;
import com.jz.beijianbao.subclass.bean.SubClass;
import com.jz.beijianbao.subclass.bean.SubClassAdd;
import com.jz.beijianbao.subclass.bean.SubClassSelectVo;
import com.jz.beijianbao.subclass.service.SubService;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@RestController
@CrossOrigin
public class TestSub {

    @Autowired
    private SubService subService;

    @PostMapping("metadata/getPullDownList")
    public ResultVO queryXiaLa(){
        List<StatusVo> statusVos = new ArrayList<>();
        StatusVo statusVo = new StatusVo(1,"启用");
        StatusVo statusVo1 = new StatusVo(0,"禁用");
        statusVos.add(statusVo);
        statusVos.add(statusVo1);
        return ResultBuildVO.success(statusVos);
    }

//  分页查询
    @PostMapping("subClass/queryPage")
    public ResultVO queryPage(@RequestBody SubClassSelectVo subClassSelectVo){
        ResultVO resultVO = subService.queryPag(subClassSelectVo);
        return resultVO;

    }
//    新增小件
    @PostMapping("subClass/add")
    public ResultVO add(@RequestBody  @Valid SubClassAdd subClassAdd, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVO resultVO = subService.add(subClassAdd);
        return resultVO;

    }
    //回显
    /*@PostMapping("echo")
    public ResultVO echo(Integer id){
        ResultVO resultVO = subService.selectById(id);
        return resultVO;
    }*/
//    回显后修改
    @PostMapping("subClass/update")
    public ResultVO update(@RequestBody  @Valid SubClassAdd subClassAdd, BindingResult bindingResult){
        ResultVO resultVO = subService.update(subClassAdd);
        return resultVO;
    }
//    修改状态
    @PostMapping("subClass/updateStatus")
    public ResultVO status(@RequestBody SubClass subClass){
        ResultVO resultVO = subService.status(subClass);
        return resultVO;
    }















}
